package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;

public class ReflectDemo7 {
    public static void main(String[] args) throws Exception {
//     File dir = new File(
//             ReflectDemo7.class.getClassLoader().getResource(".").toURI()
//     );
        //System.out.println(dir.getAbsoluteFile());
        File dir = new File(
                ReflectDemo7.class.getResource(".").toURI()
        );
        System.out.println(dir.getAbsolutePath());
        String pack = ReflectDemo7.class.getPackage().getName();
        File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File f :files) {
            String className = f.getName().substring(0, f.getName().indexOf("."));
            Class cls = Class.forName(pack+"."+className);

            System.out.println(cls.getName());
            Method[] methods = cls.getDeclaredMethods();
            for(Method method : methods) {
                if(method.getParameterCount() == 0 && method.getModifiers() == Modifier.PUBLIC) {
                    System.out.println("自动调用"+cls.getSimpleName()+"类的"+method.getName()+"方法");
                    Object o = cls.newInstance();
                    method.invoke(o);
                }
            }
        }


    }
}
